package org.springblade.modules.admin.vo.plan;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.MaintainPlanTemplate;
import org.springblade.modules.admin.enums.MaintainPlanContentEnum;
import org.springblade.modules.admin.enums.TaskStatusEnum;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zcc
 * @version 1.0
 * @date 2022/4/21 14:36
 */
@Data
public class PlanListVo {

	private String signNames;

	private String elevatorNum;

	private Long elevatorId;

	private Long planId;

	private String userSignInfos;

	private String userSignInfos2;

	List<SignStatusInfoVo> userSignInfoList;

	List<PlanUserSignDetailVo> userSignInfoList2;

	@ApiModelProperty("实际执行人")
	List<PlanUserSignDetailVo> realUserSignInfoList;

	private String groupName;

	private String buildingName;

	private String elevatorAddress;

	private String elevatorNo;

	private String planDesc;

	private String planDate;

	private String planStartTime;

	private String planEndTime;

	private Integer planStatus;

	private String planStatusName;

	private Integer overdueDay;

	private String overdueDayName;

	private String planCheckName;

	@ApiModelProperty("工作时长")
	private Integer workMinute;

	@ApiModelProperty("工单实际作业时长，单位分钟")
	private Integer workDuration;

	@ApiModelProperty("评分状态 0-未评分，1-已评分")
	private Integer scoreStatus;

	@ApiModelProperty("评分分数")
	private BigDecimal score;

	@ApiModelProperty("工作时长")
	private String workMinuteName;

	private String imageNums;

	private Integer imageCounts = 0;

	private Date createTime;

	private String mfDesc;

	private String realNames;

	@ApiModelProperty("计划执行人")
	private String personnelRealNames;

	private Integer isStop;

	private Integer deferType;

	private String deferTypeName;

	private Integer deferWarrant;

	private String deferWarrantName;

	@ApiModelProperty("延期总次数")
	private Integer deferCounts;

	@ApiModelProperty("延期文案总次数")
	private Integer deferDescCounts;

	private Integer auditType;

	private Integer elevatorAuditType;

	private Date overdueDate;

	private Date maintainSignatureTime;

	@ApiModelProperty("逾期类型1- 非年审梯,1:已逾期,2:未逾期")
	private Integer overdueType;


	@ApiModelProperty("true：即将逾期,false")
	private Boolean isOverdueTomorrow = false;

	@ApiModelProperty("电梯年审0：年审梯，非年审")
	private Integer useType;

	/**
	 * 模板名称
	 */
	private String templateNames;
	/**
	 * 计划名称
	 */
	private String taskPlanName;
	/**
	 * 计划年审类型 1-非年审 2-年审
	 */
	private Integer taskPlanAuditType;

	/**
	 * 人员id集合
	 */
	private String userIds;

	/**
	 * 人员id集合
	 */
	private String personnelUserIds;

	/**
	 * 模板名称
	 */
	private String templateIds;

	private Integer scoreType;

	private Date commitTime;

	/**
	 * 2024-06-21 新增 电梯最后排期时间
	 */
	@ApiModelProperty("电梯最后排期时间")
	@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
	private Date lastPlanDate;

	/**
	 * 2024-06-21 新增 设备类型名
	 */
	@ApiModelProperty("设备类型名")
	private String deviceTypeName;

	private String overdueTypeName;

	private String brandName;
	private String elevatorDeptNames;

	public void setOverdueType(Integer overdueType) {
		if(Func.isNotEmpty(overdueType)){
			if (overdueType == 1){
				this.overdueTypeName = "未逾期";
			}
			if (overdueType == 2){
				this.overdueTypeName = "明天即将逾期";
			}
			if (overdueType == 3){
				this.overdueTypeName = "已逾期";
			}
		}
		this.overdueType = overdueType;
	}


	@ApiModelProperty("单位内编号")
	private String registerNumber;

	public void setDeferType(Integer deferType) {
		deferType = Convert.toInt(deferType, 4);
		this.deferType = deferType;
		if (deferType == 1) {
			this.deferTypeName = "已延期";
		} else if (deferType == 2) {
			this.deferTypeName = "已提前";
		} else if (deferType == 3) {
			this.deferTypeName = "无变化";
		} else if (deferType == 4) {
			this.deferTypeName = "从未调整";
		} else if (deferType == 5) {
			this.deferTypeName = "特殊延期";
		}
	}

	public void setDeferWarrant(Integer deferWarrant) {
		deferWarrant = Convert.toInt(deferWarrant, 4);
		this.deferWarrant = deferWarrant;
		if (deferWarrant == 1) {
			this.deferWarrantName = "未提交";
		} else if (deferWarrant == 2) {
			this.deferWarrantName = "已齐全";
		} else if (deferWarrant == 3) {
			this.deferWarrantName = "未完善";
		} else if (deferWarrant == 4) {
			this.deferWarrantName = "无需提交";
		} else if (deferWarrant == 5) {
			this.deferWarrantName = "未提交";
		}
	}

	private List<PlanSignStatusVo> planSignStatusList;

	public void setUserSignInfos2(String userSignInfos2) {
		this.userSignInfos2 = userSignInfos2;
		List<PlanUserSignDetailVo> list = new ArrayList<>();
		List<PlanUserSignDetailVo> list2 = new ArrayList<>();

		if (StrUtil.isNotEmpty(userSignInfos2)) {
			String[] split = userSignInfos2.split("%");
			for (String str : split) {
				try {
					PlanUserSignDetailVo planUserSignDetailVo = JSON.parseObject(str, PlanUserSignDetailVo.class);
					list.add(planUserSignDetailVo);
					if (planUserSignDetailVo.getIsTakePartIn()) {
						list2.add(planUserSignDetailVo);
					}
				}catch ( Exception e){
					e.printStackTrace();
				}

			}
		}
		if (list.size() > 0) {
			this.userSignInfoList2 = list;
		}
		if (list2.size() > 0) {
			this.realUserSignInfoList = list2;
		}
	}

	public void setImageNums(String imageNums) {
		int imageCount = 0;
		this.imageNums = imageNums;
		if (StrUtil.isNotEmpty(imageNums)) {
			String[] split = imageNums.split("%");
			for (String str : split) {
				imageCount = imageCount + Convert.toInt(str, 0);
			}
			this.imageCounts = imageCount;
		}
	}

	public void setOverdueDay(Integer overdueDay) {
		this.overdueDay = Convert.toInt(overdueDay, 0);
		if (overdueDay > 0) {
			this.overdueDayName = "超时";
		} else {
			this.overdueDayName = "及时";
		}
	}

	public void setPlanStatus(Integer planStatus) {
		this.planStatus = planStatus;
		this.planStatusName = TaskStatusEnum.getTaskName(MaintainPlanContentEnum.MAINTAIN_BY.getId(), planStatus);
	}

	public void setUserSignInfos(String userSignInfos) {
		if (StrUtil.isNotEmpty(userSignInfos)) {
			List<SignStatusInfoVo> list = new ArrayList<>();
			String[] split = userSignInfos.split("%");
			for (String str : split) {
				SignStatusInfoVo signStatusInfoVo = JSON.parseObject(str, SignStatusInfoVo.class);
				list.add(signStatusInfoVo);
			}
			this.userSignInfoList = list;
		}
		this.userSignInfos = userSignInfos;

	}

	public void setWorkDuration(Integer workDuration) {
		this.workDuration = workDuration;
		if (workDuration != null) {
			if (workDuration < 25) {
				this.workMinuteName = "时间过短";
			} else if (workDuration > 60) {
				this.workMinuteName = "时间过长";
			} else {
				this.workMinuteName = "正常";
			}
		}
	}

	public void setGroupName(String groupName) {
		if(Func.isNotEmpty(groupName) && groupName.contains("%")){
			List<String> strings = Func.toStrList(groupName);
			strings = strings.stream().map(e -> e.split("%")[1]).collect(Collectors.toList());
			this.groupName = String.join(",",strings);
		}else {
			this.groupName = groupName;
		}
	}

	public void setTemplateNames(String templateNames) {
		if(Func.isNotEmpty(templateNames) && templateNames.contains("%")){
			List<String> strings = Func.toStrList(templateNames);
			List<String> planCheckNames = new ArrayList<>(16);
			List<MaintainPlanTemplate> list = new ArrayList<>(10);
			for (String str :strings) {
				String[] split = str.split("%");
				MaintainPlanTemplate maintainPlanTemplate = new MaintainPlanTemplate();
				maintainPlanTemplate.setTemplateName(split[0] + "-" + split[1]);
				planCheckNames.add(split[1]);
				maintainPlanTemplate.setSort(Convert.toInt(split[2],0));
				list.add(maintainPlanTemplate);
			}
			List<String> collect = list.stream().sorted(Comparator.comparing(MaintainPlanTemplate::getSort)).map(MaintainPlanTemplate::getTemplateName).collect(Collectors.toList());
			this.templateNames = String.join(",",collect);
			this.planCheckName = String.join(",",planCheckNames);
		}else {
			this.templateNames = templateNames;
		}
	}

	public void setRealNames(String realNames) {
		if(Func.isNotEmpty(realNames) && realNames.contains("%")){
			List<String> strings = Func.toStrList(realNames);
			strings = strings.stream().map(e -> e.split("%")[1]).collect(Collectors.toList());
			this.realNames = String.join(",",strings);
		}else {
			this.realNames = realNames;
		}
	}

	public void setPersonnelRealNames(String personnelRealNames) {
		if(Func.isNotEmpty(personnelRealNames) && personnelRealNames.contains("%")){
			List<String> strings = Func.toStrList(personnelRealNames);
			strings = strings.stream().map(e -> e.split("%")[1]).collect(Collectors.toList());
			this.personnelRealNames = String.join(",",strings);
		}else {
			this.personnelRealNames = personnelRealNames;
		}
	}

	public void setElevatorAddress(String elevatorAddress) {
		this.elevatorAddress = elevatorAddress;
		this.elevatorNo = elevatorAddress;
	}
}
